# -*- coding: utf-8 -*-
import scrapy


class CfSpider(scrapy.Spider):
    name = 'cf'
    allowed_domains = ['circ.gov.cn']
    start_urls = ['http://www.circ.gov.cn/web/site0/tab5240/module14430/page1.htm']

    def parse(self, response):
        #//table[@id='ess_ctr14430_ListC_Info_LstC_Info']//tr
        #//table[@id='ess_ctr14430_ListC_Info_LstC_Info']//tr//td[@class='hui14']//a/text()
        tr_list = response.xpath("//table[@id='ess_ctr14430_ListC_Info_LstC_Info']/tr/td//tr")
        for tr in tr_list:
            item={}
            item["title"]=tr.xpath("./td[@class='hui14']/span/a/text()").extract_first()
            item["publish_date"]=tr.xpath("./td[3]/text()").extract_first()
            # print(item)
            yield item
        next_url=response.xpath("//a[text()='下一页']/@href").extract_first()
        # print(response.body.decode("utf-8"))
        # print(next_url)
        if next_url is not None:
            next_url="http://www.circ.gov.cn"+next_url
            print(next_url)
            yield scrapy.Request(
                next_url,
                callback=self.parse,
            )
        else:
            # print(item)
            yield item